package com.yj.smarthome.test.ipcamera;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;

import com.yj.smarthome.camera.HCNetSDK.byte96;

public class H264Camera {

	public static String command = "GET http://192.168.1.158:80/livestream/11?action=play&media=video HTTP/1.1\r\n"
			+ "User-Agent: HiIpcam/V100R003 VodClient/1.0.0\r\n" + "Connection: Keep-Alive\r\n" + "Cache-Control: no-cache\r\n"
			+ "Authorization: admin 123456 \r\n" + "Content-Length: 100 \r\n" + "\r\n" + "Cseq: 1\r\n"
			+ "Transport: RTP/AVP/TCP;unicast;interleaved=0-1\r\n" + "\r\n";

	public static String host = "192.168.1.158";
	public static int port = 80;
	public static int number = 11;
	public static String type = "video";
	public static String username = "admin";
	public static String password = "123456";
	public static int length;

	public static void main(String[] args) {
		try {
//			InetSocketAddress isa = new InetSocketAddress(host, port);
//			Socket socket = new Socket();
//			socket.connect(isa, 2000);

			Socket socket = new Socket(host, port);

			new ReaderThread(socket).start();

			Thread.sleep(1000);

//			DataOutputStream bos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
//			bos.writeUTF(command);

			socket.getOutputStream().write(command.getBytes());

//			StringBuffer sb = new StringBuffer();
////			sb.append("GET http://"+host+":"+port+"/livestream/"+number+"?action=play&media=video_audio_data HTTP/1.1\r\n");
//			sb.append("GET http://192.168.1.158:80/livestream/11?action=play&media=video_audio_data HTTP/1.1\r\n");
//			sb.append("User-Agent: HiIpcam/V100R003 VodClient/1.0.0\r\n");
//			sb.append("Connection: Keep-Alive\r\n");
//			sb.append("Cache-Control: no-cache\r\n");
////			sb.append("Authorization: "+username+" "+password+"\r\n");
//			sb.append("Authorization: admin 123456\r\n");
//			sb.append("Content-Length: 100\r\n");
//			sb.append("Cseq: 1\r\n");
//			sb.append("Transport: RTP/AVP/TCP;unicast;interleaved=0-1\r\n");
//			sb.append("\r\n");
////			bos.writeBytes(sb.toString());
//			bos.writeUTF(sb.toString());

//			bos.writeUTF("GET http://"+host+":"+port+"/livestream/"+number+"?action=play&media=video_audio_data HTTP/1.1\r\n");
//			bos.writeUTF("User-Agent: HiIpcam/V100R003 VodClient/1.0.0\r\n");
//			bos.writeUTF("Connection: Keep-Alive\r\n");
//			bos.writeUTF("Cache-Control: no-cache\r\n");
//			bos.writeUTF("Authorization: "+username+" "+password+"\r\n");
//			bos.writeUTF("Content-Length: 57\r\n");
//			bos.writeUTF("Cseq: 1\r\n");
//			bos.writeUTF("Transport: RTP/AVP/TCP;unicast;interleaved=0-1\r\n");
//			bos.writeUTF("\r\n");

//			BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(socket.getInputStream())));
//			String line;
//			while((line = reader.readLine())!=null){
//				System.out.println(line);
//			}

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

class ReaderThread extends Thread {

	private Socket socket;
	private BufferedReader reader;
	private BufferedInputStream bis;

	public ReaderThread(Socket socket) {
		this.socket = socket;
		try {
			this.bis = new BufferedInputStream(socket.getInputStream());
//			this.reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(socket.getInputStream())));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		String line;
		int len;
		byte[] bytes = new byte[1024];
		while (true) {
			try {
//				line = reader.readLine();
//				if (line != null) {
//					System.out.println(line);
//				}

				while ((len = bis.read(bytes)) != -1) {
					System.out.println(Arrays.toString(bytes));
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
